<?php

namespace WeForge\Runtime\Http\Middleware;

class HttpsRedirect
{
    /**
     * Handle the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  callable  $next
     * @return mixed
     */
    public function handle($request, $next)
    {
        if (($_ENV['HTTPS'] ?? null) === 'on' && $request->header('X-Forwarded-Proto') === 'http') {
            return redirect()->secure($request->getRequestUri());
        }

        return $next($request);
    }
}
